package com.lebang.controller.api;

import com.lebang.aop.Sessionid;
import com.lebang.common.MemberLock;
import com.lebang.common.Require;
import com.lebang.domain.MemberPointInfo;
import com.lebang.domain.MemberPointRecord;
import com.lebang.domain.PointCountSetting;
import com.lebang.entity.RequestHeaders;
import com.lebang.model.MoneyCategory;
import com.lebang.model.Whether;
import com.lebang.model.enums.buss.TaskCategory;
import com.lebang.service.PointService;
import com.lebang.service.buss.MemberTradeService;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

/**
 * @Author: hwq
 * @Date: 2019/11/19 15:55
 * @Version 1.0
 */
@Api(tags = "金币相关方法")
@RestController
@RequestMapping("/point/member")
public class PointApi {

    @Autowired
    private MemberTradeService tradeService;
    @Autowired
    private PointService pointService;
    public static final String KEY = "memberpoint:";

    @ApiOperation("获取金币余额")
    @Sessionid
    @Require
    @PostMapping("/wall")
    @MemberLock
    public JsonData wall(HttpServletRequest request) {
        RequestHeaders requestHeaders = new RequestHeaders(request);
        String memberId = requestHeaders.getUserid();
        try {
            MemberPointInfo point = pointService.getWallet(memberId);
            return Callback.success("success", "查询成功", point);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Callback.success("success", "查询成功", null);
    }

    /**
     * 金币兑换余额
     *
     * @param request
     * @param settingId
     * @return
     */
    @ApiOperation("金币兑换余额")
    @PostMapping("/cash")
    @Sessionid
    @Require
    @MemberLock
    public JsonData cash(HttpServletRequest request, Integer settingId) {
        RequestHeaders requestHeaders = new RequestHeaders(request);
        String memberId = requestHeaders.getUserid();
        PointCountSetting setting = pointService.selectSettingById(settingId);
        if (setting == null) {
            return Callback.fail("200", "兑换比例不存在");
        }
        try {
            MemberPointInfo info = pointService.getWallet(memberId);
            if (info.getCount() < setting.getPoint()) {
                return Callback.fail("200", "可用金币不足");
            } else {
                boolean o = pointService.pointCash(memberId, setting.getPoint(), setting);
                if (o) {
                    tradeService.addMoney(memberId, setting.getCount(),
                            "金币兑换", setting.getPoint() + "金币兑换"
                                    + new BigDecimal(setting.getCount()).divide(new BigDecimal(100)),
                            "", new RequestHeaders(request), "", new MoneyCategory(Whether.YES, TaskCategory.POINT));
                    return Callback.success("200", "兑换成功");
                } else {
                    return Callback.success("200", "兑换失败");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Callback.fail("200", "请求过于频繁");
        }
    }

    /**
     * 金币兑换配置
     *
     * @param request
     * @return
     */
    @ApiOperation("金币兑换配置")
    @Sessionid
    @Require
    @PostMapping("/setting/list")
    public JsonData list(HttpServletRequest request) {
        return Callback.success("success", "查询成功", pointService.selectSetting());
    }


    /**
     * 金币兑换流水
     *
     * @param request
     * @return
     */
    @ApiOperation("金币兑换流水")
    @Sessionid
    @Require
    @PostMapping("/cash/record")
    public JsonData record(HttpServletRequest request, Long page, Integer Limit) {
        RequestHeaders requestHeaders = new RequestHeaders(request);
        String memberId = requestHeaders.getUserid();
        List<MemberPointRecord> data = pointService.selectRecordByMemberId(memberId, 1);
        return Callback.success("success", "查询成功", data);
    }

    /**
     * 金币兑换流水
     *
     * @param request
     * @return
     */
    @ApiOperation("金币收入")
    @Sessionid
    @Require
    @PostMapping("/trade/record")
    public JsonData cashrecord(HttpServletRequest request, Long page, Integer Limit) {
        RequestHeaders requestHeaders = new RequestHeaders(request);
        String memberId = requestHeaders.getUserid();
        List<MemberPointRecord> data = pointService.selectTradeRecordByMemberId(memberId, 0, page, Limit);
        return Callback.success("success", "查询成功", data);
    }
}
